import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class GetParams {
	private String video;
	private String plid;
	private Long startTime;
	public double st;
	public double et;
	public double len;
	NTPTime time;
	public String on3g = "1";
	public String app = "youtube_mobile";
	public String preq = "/watch?ajax=1&layout=tablet&tsp=1&utcoffset=120&v=";
	public String reloadCount = "0";
	public String html5 = "1";
	public String ns = "yt";
	public String ps = "blazer";
	public String el = "detailpage";
	public String hl = "en";
	public String cr = "US";
	public String fmt = "18";
	public String fs = "0";
	public String screenw = "768";
	public String screenh = "1024";
	public String w = "768";
	public String h = "460";
	public String vw = "0";
	public String vh = "0";
	public String playerw = "768";
	public String playerh = "460";
	public String sw = "1";
	public String noflv = "1";
	public String skl = "false";
	public String build_id = "";
	public String playback = "0";
	public String referrer = "";

	GetParams(String video, double lenght, String build_id) {
		this.video = video;
		this.len = lenght;
		this.build_id = build_id;
		this.st = 0.0D;
		this.et = 0.0D;

		this.time = new NTPTime("0.pool.ntp.org");

		generatePlid();
	}

	public Map<String, String> generateData(boolean mYoutube)
			throws UnsupportedEncodingException {
		Map data = new HashMap();
		data.put("app", this.app);
		data.put("html5", this.html5);
		data.put("plid", this.plid);
		data.put("fmt", this.fmt);
		data.put("ns", this.ns);
		data.put("el", this.el);
		data.put("ps", this.ps);
		if (!this.playback.equals("0")) {
			data.put("playback", this.playback);
		}

		if (!this.referrer.equals("")) {
			data.put("referrer", URLEncoder.encode(this.referrer, "UTF-8"));
		}

		if (mYoutube) {
			data.put("noflv", this.noflv);
			data.put("video_id", this.video);
			data.put("skl", this.skl);
		} else {
			data.put("docid", this.video);
			data.put("on3g", this.on3g);
			data.put("preq", URLEncoder.encode(this.preq + this.video, "UTF-8"));
			data.put("reloadCount", this.reloadCount);
			data.put("rdm", getRdm());
			data.put("hl", this.hl);
			data.put("cr", this.cr);
			data.put("fs", this.fs);
			data.put("screenw", this.screenw);
			data.put("screenh", this.screenh);
			data.put("w", this.w);
			data.put("h", this.h);
			data.put("vw", this.vw);
			data.put("vh", this.vh);
			data.put("playerw", this.playerw);
			data.put("playerh", this.playerh);
			data.put("sw", this.sw);
			data.put("len", String.valueOf(this.len));
			data.put("st", String.valueOf(this.st));
			data.put("et", String.valueOf(this.et));
			data.put("rt", getRt());
		}

		System.out.println(data.toString());

		return data;
	}

	private String getRt() {
		Long rt = Long.valueOf(this.time.getTime().longValue()
				- this.startTime.longValue());
		return String.valueOf(Double.valueOf(Long.toString(rt.longValue()))
				.doubleValue() / 1000.0D);
	}

	private String getRdm() {
		String rand = String.valueOf(new Random().nextInt(8999) + 1000);
		return Base36.encode(new BigInteger(this.build_id))
				+ Base36.encode(new BigInteger(rand));
	}

	private void generatePlid() {
		this.startTime = this.time.getTime();

		long min = 1000000000000L;
		long max = 9999999999999L;
		Long random = Long.valueOf(min
				+ (new Random().nextLong() * (max - min)));

		this.plid = (Base36.encode(new BigInteger(new StringBuilder().append(
				this.startTime).toString())) + Base36.encode(new BigInteger(
				new StringBuilder().append(random).toString())));
	}

	public void updateStEt() {
		this.st = this.et;
		this.et += 10.0D;
		Random rand = new Random();
		if (rand.nextBoolean())
			this.et -= rand.nextDouble();
		else {
			this.et += rand.nextDouble();
		}
		this.et = (Double.valueOf(
				Integer.valueOf(Long.toString(Math.round(this.et * 1000.0D)))
						.intValue()).doubleValue() / 1000.0D);
	}

	public Map<String, String> generateData(boolean b, boolean c)
			throws UnsupportedEncodingException {
		this.playback = "1";
		Map data = generateData(b);
		this.playback = "0";
		return data;
	}

	public Map<String, String> generateData(boolean b, boolean c,
			String referrer) throws UnsupportedEncodingException {
		this.referrer = referrer;
		Map data = generateData(b, c);
		this.referrer = "";
		return data;
	}

	public Map<String, String> generateData(boolean b, String referrer)
			throws UnsupportedEncodingException {
		this.referrer = referrer;
		Map data = generateData(b);
		this.referrer = "";
		return data;
	}
}